<?php

namespace App\Controllers;

use App\Controller;
use JsonLowCode\Package;
use JsonLowCode\UI;

class Base extends Controller
{
    public function basic(): UI\Space
    {
        return UI::space([
            UI::text("测试内容"),
            UI::button("default"),
            UI::button("tertiary")->tertiary(),
            UI::button("primary")->primary(),
            UI::button("info")->info(),
            UI::button("warning")->warning(),
            UI::button("success")->success(),
            UI::button("error")->error()
        ]);
    }

    public function basic_size(): UI\Space
    {
        return UI::space([
            UI::button("tiny")->tiny(),
            UI::button("small")->small(),
            UI::button("medium")->medium(),
            UI::button("default")->default(),
            UI::button("large")->large(),
        ]);
    }



    public function index()
    {
        $page = UI::page(function (UI\Page $page){
            $bindName1 = "state.code_".uniqid();
            $bindName2 = "state.code_".uniqid();
            $bindName3 = "state.code_".uniqid();
            $buttonCard1 = UI::appCode("基础按钮",getObjectCode(self::class,'basic'), $bindName1)->children($this->basic());
            $buttonCard2 = UI::appCode("按钮尺寸",getObjectCode(self::class,'basic_size'), $bindName2)->children($this->basic_size());
            $grid = UI::grid(2,12,12);
            $grid->item($buttonCard1);
            $grid->item($buttonCard2);
            $code = UI::appCode("页面标题",getObjectCode(self::class),$bindName3)->type(2)->class("shadow-sm")->children($grid);
            $code->prop("height","calc(100vh - 172px)");
            $page->children($code);
            $page->setState("testSchema", json_encode($this->testPage(),JSON_UNESCAPED_UNICODE));
            $page->children(UI::html()->vBind("schema","JSON.parse(state.testSchema)")->componentName("SchemaRender"));
        });
        $content = UI::content($page);
        $content->packages($this->packages());
        return $this->result(compact("content"));
    }

    public function testPage(){
        $page = UI::page(function(UI\Page $page){
            $page->setCacheKey();
            $page->setState("user",[
                "userName"=>"小米",
                "age"=>13
            ],$page->getCacheKey());
            $page->setState("test","测试");
            $grid = UI::grid(2,12,12);
            $page->setAttribute("properties",[
                ["property"=>"test1","type"=>"String","defaultValue"=>"1111"]
            ]);
            $grid->item(UI::space([
                UI::button("测试")->primary()->setCacheKey($page->getCacheKey())->onClick("function(){ console.log(this); }"),
                UI::inputNumber()->bindModel("value","state.user.age",16),
                UI::input()->bindModel("value","state.user.userName","测试"),
                UI::sfcLocal("test",["test"=>"测试来了"]),
                UI::text("{{test1}}")
            ]));
            $page->children($grid);
        });
        return $page;
    }

    protected function packages(){
        $package = new Package();
        $package->package("element-plus")
            ->url("https://unpkg.com/element-plus@2.7.3/dist/index.full.js")
            ->library("ElementPlus")->version("2.7.3");
        return [$package];
    }

}